What is iso-639-1?
The iso-639-1 npm package provides utilities for working with ISO 639-1 language codes. It allows you to get language names from codes, get codes from language names, and check if a language code is valid.
What are iso-639-1's main functionalities?
Get Language Name from Code
This feature allows you to get the name of a language given its ISO 639-1 code. In the example, the code 'en' returns 'English'.
const ISO6391 = require('iso-639-1');
const languageName = ISO6391.getName('en');
console.log(languageName); // Output: English
Get Language Code from Name
This feature allows you to get the ISO 639-1 code of a language given its name. In the example, the name 'English' returns the code 'en'.
const ISO6391 = require('iso-639-1');
const languageCode = ISO6391.getCode('English');
console.log(languageCode); // Output: en
Check if a Language Code is Valid
This feature allows you to check if a given ISO 639-1 code is valid. In the example, the code 'en' is validated and returns true.
const ISO6391 = require('iso-639-1');
const isValid = ISO6391.validate('en');
console.log(isValid); // Output: true
Get All Language Codes
This feature allows you to get an array of all ISO 639-1 language codes. The example returns an array containing all the codes.
const ISO6391 = require('iso-639-1');
const allCodes = ISO6391.getAllCodes();
console.log(allCodes); // Output: Array of all ISO 639-1 language codes
Get All Language Names
This feature allows you to get an array of all ISO 639-1 language names. The example returns an array containing all the names.
const ISO6391 = require('iso-639-1');
const allNames = ISO6391.getAllNames();
console.log(allNames); // Output: Array of all ISO 639-1 language names
Other packages similar to iso-639-1
iso-639
The iso-639 package provides similar functionality for working with ISO 639 language codes, including both ISO 639-1 and ISO 639-2. It offers methods to get language names, codes, and validate codes. Compared to iso-639-1, it supports a broader range of language codes.
language-codes
The language-codes package provides a comprehensive list of language codes and their corresponding names, including ISO 639-1, ISO 639-2, and ISO 639-3. It offers more extensive data and supports multiple standards, making it more versatile than iso-639-1.
langs
The langs package provides utilities for working with language codes, including ISO 639-1, ISO 639-2, and ISO 639-3. It offers methods to get language names, codes, and validate codes. It is more comprehensive than iso-639-1, supporting multiple ISO standards.
ISO-639-1
Simple interface for ISO-639-1 language codes
Installation
npm install iso-639-1
Usage
Node.js
const ISO6391 = require('iso-639-1');
console.log(ISO6391.getName('en'));
ES Module
import ISO6391 from 'iso-639-1';
console.log(ISO6391.getName('en'));
Browsers
HTML
<script type="text/javascript" src="./node_modules/iso-639-1/build/index.js"></script>
Visit global variable ISO6391 in js
console.log(ISO6391.getName('en'));
Methods
getName(code)
- @param code {string}
- @return {string}
Lookup language english name by code
getAllNames()
Get array of all language english names
getNativeName(code)
- @param code {string}
- @return {string}
Lookup language native name by code
getAllNativeNames()
Get array of all language native names
getCode(name)
- @param name {string}
- @return {string}
Lookup code by english name or native name
getAllCodes()
Get array of all codes
validate(code)
- @param code {string}
- @return {boolean}
Check whether the given code is in the list of ISO-639-1
getLanguages(codes)
- @param codes {array}
- @return {array}
Get the array of the language objects by the given codes
Example
const ISO6391 = require('iso-639-1')
console.log(ISO6391.getName('zh')) // 'Chinese'
console.log(ISO6391.getNativeName('zh')) // '中文'
console.log(ISO6391.getAllNames()) // ['Afar','Abkhaz', ... ,'Zulu']
console.log(ISO6391.getAllNativeNames()) //['Afaraf','аҧсуа бызшәа', ... ,'isiZulu' ]
console.log(ISO6391.getCode('Chinese')) // 'zh'
console.log(ISO6391.getCode('中文')) // 'zh'
console.log(ISO6391.getAllCodes()) //['aa','ab',...,'zu']
console.log(ISO6391.validate('en')) // true
console.log(ISO6391.validate('xx')) // false
console.log(ISO6391.getLanguages(['en', 'zh']))
// [{code:'en',name:'English',nativeName:'English'},{code:'zh',name:'Chinese',nativeName:'中文'}]